<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/05/12
 * Time: 15:19
 */

namespace Home\Controller;


class VersionsController extends HomeController
{

    public function index(){
        $this->display();
    }

    /**
     * 版本列表json接口
     * @param $limit
     * @param $offset
     * @param null $search
     */
    public function versionsList($limit,$offset,$search=null){

        if (!empty($search)){
            $map['version_code'] = array('like', "%{$search}%");
        }
        $record = M('VersionUpgrade')->limit($limit,$offset)->where($map)->order('version_code desc')->select();
        $total_count = M('VersionUpgrade')->where($map)->count();
        empty($total_count) && $total_count = 0;
        $result['rows'] = $record;
        $result['total'] = $total_count;
        $this->ajaxReturn($result);
    }

    public function delete(){
        $ids = I('selectedIds');
        $where['version_upgrade_id'] = array('in', $ids);
        $res = M('VersionUpgrade')->where($where)->delete();
        if($res !== false){
            logOperation('版本升级管理','成功删除版本记录');
            $this->sucessJson();
        }else{
            $this->errorJson('删除失败');
        }
    }
    public function createVersion(){
        if (IS_POST) {
            $v = M('VersionUpgrade');
            if ($v->field('versionCode','versionName','isMandatoryUpgrade','upgradeInstructions','downloadPath','platform')->create($_POST)) {
                $id= $v->add();
                if($id>0){
                    logOperation('版本升级管理','新增版本成功');
                    $this->sucessJson();
                }
            }
            $this->errorJson('添加失败');
        }
    }
}